<div class="layui-fluid" style="top: 0;padding-top: 0">
    <div class="layui-col-xs12">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form resAddForms" action="" lay-filter="resAddForms">
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin:0;">
                            <a lay-submit lay-filter="resAddeditForm" class="layui-btn">保存</a>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">资源名称:<span style="color:red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" name="resName" autocomplete="off"
                                   class="layui-input resNames" maxlength="32" lay-verify="required|names">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">资源别名:<span style="color:red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" maxlength="32" autocomplete="off"
                                   class="layui-input alias" lay-verify="required|alias">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">资源图标:</label>
                        <div class="layui-input-block" style="line-height: 36px">
                            <i class="" id="iconShow"></i>
                            <span class="layui-btn iconSelect">请选择</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">默认地址:</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="defaultUrl" lay-verify="defaultUrl">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">是否有子节点:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="isFolder" value="1" title="是" checked>
                            <input type="radio" name="isFolder" value="0" title="否">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">显示到菜单:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="isDisplayInMenu" value="1" title="是" checked>
                            <input type="radio" name="isDisplayInMenu" value="0" title="否">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">默认打开:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="isOpen" value="1" title="是" checked>
                            <input type="radio" name="isOpen" value="0" title="否">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否打开新窗口:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="isNewOpen" value="1" title="是">
                            <input type="radio" name="isNewOpen" value="0" title="否" checked>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">同级排序:<span style="color:red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" name="sn" class="layui-input" lay-verify="required|sort">

                        </div>
                    </div>

                    <input type="text" name="parentId" class="layui-input layui-hide">
                    <input type="text" name="systemId" class="layui-input layui-hide">
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'layer', 'table', 'commons', 'setter', 'jquery'], function () {
        var form = layui.form, layer = layui.layer, commons = layui.commons, setter = layui.setter, $ = layui.jquery;
        form.render();
        form.verify(commons.formVerify);
        var resId = $('#resIdSelect').val();
        var parentIds = $('#resParentId').val();
        var orgId = $('#orgIdSelect').val();
        if (parentIds) {
            form.on('submit(resAddeditForm)', function (data) {
                var iconClassName = "";
                iconClassName = $.trim($("#iconShow").attr('class')).split(" ");
                data.field.iconSkin = iconClassName[2];
                data.field.resId = resId;
                data.field.orgId = orgId;
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "sysRes/add",
                    type: "POST",
                    dataType: 'json',
                    data: data.field,
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                            layer.alert(data.msg, function (index) {
                                layer.close(index);
                                layui.resZteeRef();
                                $('#viewInformation').empty();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                });
            });
            //数据回写
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "sysRes/get",
                type: "get",
                dataType: 'json',
                data: {
                    resId: resId
                },
                success: function (data) {
                    var returnDate = data.data;

                    var creator = returnDate.creator;
                    var official="0";
                    if(creator==='999999999'){
                        official="1";
                    }
                    form.val('resAddForms', {
                        "resName": returnDate.resName,// "name": "value"
                        "alias": returnDate.alias,//复选框选中状态
                        "isFolder": returnDate.isFolder + "",
                        "defaultUrl": returnDate.defaultUrl,
                        "isDisplayInMenu": returnDate.isDisplayInMenu + "",
                        "isOpen": returnDate.isOpen + "",
                        "isNewOpen": returnDate.isNewOpen + "",
                        "sn": returnDate.sn + "",
                        "parentId": returnDate.parentId + "",
                        "systemId": returnDate.systemId + "",
                        "official": official + "",
                        "roleType": returnDate.roleType + ""
                    });
                    $('#iconShow').addClass(returnDate.iconSkin);
                }
            });
        } else {
            form.on('submit(resAddeditForm)', function (data) {
                var iconClassName = "";
                iconClassName = $.trim($('#iconShow').attr('class')).split(" ");
                data.field.iconSkin = iconClassName[2];
                data.field.parentId = resId;
                data.field.orgId = orgId;
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "sysRes/add",
                    type: "POST",
                    dataType: 'json',
                    data: data.field,
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                           layer.alert(data.msg, function (index) {
                                layer.close(index);
                                layui.resZteeRef();
                                $('#viewInformation').empty();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                });
            });
        }
        $(".iconSelect").click(function () {
            layer.open({
                type: 2,
                area: ['860px', '600px'],
                title: ['选择图标'],
                content: serverPath.systemPath  + setter.sysLinkPath + 'sysRes/view/icons'
            });
        });
        //自动生成别名
        $(".resNames").change(function () {

            var aliasExist = $(".alias").val();
            if(aliasExist || $.trim(aliasExist).length>0){
                return;
            }

            var str = $(this).val();
            if (str == "") return;
            var arrRslt = component.chineseToPinyinAlias(str);
            $(".alias").val(arrRslt[0]);
        });
    })
</script>
<style scoped>

    /* sysRes/add */
    .resAddForms .layui-form-label {
        width: 120px;
    }
    .resAddForms .layui-input-block {
        margin-left: 160px;
    }
</style>